package com.ibm.news.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class FileOperation {

	/**
	 * FileOperation 类默认的构造方法。
	 */
	public FileOperation() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * readFile 方法为读取一个文件的内容，并将内容以字符串形式返回。
	 * @param f 是需要读取的文件对象。
	 * @return 返回文件内容的字符串对象。
	 */
	/*public static String readFile(File f) {
		if (!f.exists() || !f.isFile()) {
			System.out.println("文件不存在或者是目录!");
			return null;
		}
		String fileContent = "";
		
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(f), "gbk");
			BufferedReader br = new BufferedReader(isr);
			String line = "";
			while ((line = br.readLine()) != null) {
				fileContent += line + "\r\n";
			}
			br.close();
			isr.close();
			return fileContent;
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	*/
	
	
	/**
	 * readFile 方法为读取一个文件的内容，并将内容以字符串形式返回。
	 * @param f 是需要读取的文件对象。
	 * @return 返回文件内容的字符串对象。
	 */
	public static List<String> readFile(File f) {
		if (!f.exists() || !f.isFile()) {
			System.out.println("文件不存在或者是目录!");
			return null;
		}
		//System.out.println(CharacterEncoding.get_charset(f));
		String charSet = CharacterEncoding.get_charset(f);
		List<String> listFileContent = new ArrayList<String>();
		
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(f), charSet);
			BufferedReader br = new BufferedReader(isr);
			String line = "";
			while ((line = br.readLine()) != null) {
				//listFileContent.add(line);
				//System.out.println(line.getBytes());
				listFileContent.add(CharacterEncoding.stringToUnicode(line));
				System.out.println(CharacterEncoding.stringToUnicode(line));
			}
			//System.out.println(CharacterEncoding.getEncoding(FilesConvert.listArrayToSting(listFileContent)));
			br.close();
			isr.close();
			return listFileContent;
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
		
	/**
	 * writeFile 方法是将一个给定的字符串内容，指定文件中。
	 * @param f 是写入的新的文件对象。
	 * @param fileContent 指定将要写入文件的字符串内容。
	 * @return 返回文件是否写入成功。
	 */
	public static boolean writeFile(File f, String fileContent) {
		if (!f.exists() || !f.isFile()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
			
		try {
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
			BufferedWriter bw = new BufferedWriter(osw);
			//bw.write(fileContent);
			
			bw.write(CharacterEncoding.unicodeToString(fileContent));
			bw.close();
			osw.close();
			return true;
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public static void main(String[] args) {
		File f = new File(".");
		//String [] fileContents = FileOperation.readFile(f);
		//String fileContents = FileOperation.readFile(f);
		//System.out.println(fileContents);
		//System.out.println(FileOperation.writeFile(new File("d:\\index.jsp"), FilesConvert.stringArrayToSting(fileContents)));
		//System.out.println(FileOperation.writeFile(new File("d:\\index.jsp"), fileContents));
		//List<String> fileContents = FileOperation.readFile(f);
		//System.out.println(FileOperation.writeFile(new File("d:\\index.jsp"), FilesConvert.listArrayToSting(fileContents)));
		//fileContents = FileOperation.readFile(new File("d:\\index.jsp"));
		/*List<File> listFile = FilesConvert.getTheTypeAllFiles(f, "html");
		for (File file : listFile) {
			FilesConvert.replaceContentAllFiles(file, ".jsp", ".jsp");
			FilesConvert.replaceContentAllFiles(file, "charset=gb2312", "charset=utf-8");
		}
		FilesConvert.SourceTypeToTargetType("html", "jsp", f);*/
		List<File> listFile = FilesConvert.getTheTypeAllFiles(f, "jsp");
		for (File file : listFile) {
			List<String> listFileContent = readFile(file);
			String s =  "<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%>\r\n" + FilesConvert.listArrayToSting(listFileContent);			
			if (writeFile(file, s)) {
				System.out.println("文件修改成功");
			} else {
				System.out.println("文件修改失败");
			}
		}
		
		
	}
}
